package top.jle.wx.proxy.facade.impl;

import com.alibaba.fastjson2.JSONObject;
import top.jle.jpa.basic.util.ExceptionUtils;
import top.jle.wx.proxy.exception.ExceptionCode;
import top.jle.wx.proxy.facade.BasicFacade;
import top.jle.wx.proxy.facade.ToolFacade;
import top.jle.wx.proxy.feign.ResultCheckUtil;
import top.jle.wx.proxy.feign.tool.ToolAPIFeign;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.jle.wx.proxy.constant.ToolOption;

@Component("toolFacade")
public class ToolFacadeImpl implements ToolFacade {

    @Autowired
    private ToolAPIFeign toolAPIFeign;

    @Autowired
    private BasicFacade basicFacade;

    @Override
    public ToolOption.QRCodeResponse createLimitQRCode(String sceneCode){

        if(StringUtils.isBlank(sceneCode)){
            ExceptionUtils.throwInvalidParamException();
        }

        String optionStr = "{\"action_name\": \""+ ToolOption.ACTION_LIMIT_STR_QR+"\", \"action_info\": {\"scene\": {\"scene_str\": \""+sceneCode+"\"}}}";
        JSONObject option = JSONObject.parseObject(optionStr);
        JSONObject result = toolAPIFeign.createQRCode(option);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.CREATE_LIMIT_QRCODE_FAIL);
        }
        return result.toJavaObject(ToolOption.QRCodeResponse.class);

    }

    @Override
    public ToolOption.QRCodeResponse createQRCode(String sceneCode, Integer expireSeconds){

        if(StringUtils.isBlank(sceneCode)){
            ExceptionUtils.throwInvalidParamException();
        }
        if(expireSeconds==null || expireSeconds < ToolOption.TEMP_QR_EXPIRE_SECONDS_DEFAULT){
            expireSeconds = ToolOption.TEMP_QR_EXPIRE_SECONDS_DEFAULT;
        }else if(expireSeconds> ToolOption.TEMP_QR_EXPIRE_SECONDS_LIMIT){
            expireSeconds = ToolOption.TEMP_QR_EXPIRE_SECONDS_LIMIT;
        }

        String optionStr = "{\"expire_seconds\": "+expireSeconds+",\"action_name\": \""+ ToolOption.ACTION_STR_QR+"\", \"action_info\": {\"scene\": {\"scene_str\": \""+sceneCode+"\"}}}";
        JSONObject option = JSONObject.parseObject(optionStr);
        JSONObject result = toolAPIFeign.createQRCode(option);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.CREATE_QRCODE_FAIL);
        }
        return result.toJavaObject(ToolOption.QRCodeResponse.class);

    }

    @Override
    public String shortUrl(String longUrl){
        if(StringUtils.isBlank(longUrl)){
            ExceptionUtils.throwInvalidParamException();
        }
        JSONObject option = new JSONObject();
        option.put("action","long2short");
        option.put("long_url",longUrl);
        JSONObject result = toolAPIFeign.shortUrl(option);
        if(!ResultCheckUtil.check(result)){
            ExceptionUtils.throwException(ExceptionCode.CREATE_QRCODE_FAIL);
        }
        return result.getString("short_url");
    }
}
